package hut.game.unit;

import hut.game.table.BanCo;
import hut.game.table.VanCo;
import android.widget.ImageView;

public class Phao extends QuanCo {
	public Phao() {
		super();
	}
	
	@Override
	public void KhoiTao(int phe, String name, String thutu, int stt,
			boolean khoa, int x, int y) {
		super.KhoiTao(phe, name, thutu, stt, khoa, x, y);
	}
	private int[][][] score = {
            {
               {50, 50, 50, 50, 50, 50, 50, 50, 50}, 		/* RED */
               {50, 50, 50, 50, 50, 50, 50, 50, 50},
               {50, 51, 53, 53, 55, 53, 53, 51, 50},
               {50, 51, 50, 50, 50, 50, 50, 51, 50},
               {50, 51, 51, 51, 51, 51, 51, 51, 50},
               {50, 51, 50, 50, 50, 50, 50, 51, 50},
               {50, 51, 50, 50, 50, 50, 50, 51, 50},
               {50, 51, 50, 50, 50, 50, 50, 51, 50},
               {50, 51, 50, 50, 50, 50, 50, 51, 50},
               {50, 50, 50, 50, 50, 50, 50, 50, 50}
            },
            {
               {50, 50, 50, 50, 50, 50, 50, 50, 50}, 		/* BLACK */
               {50, 51, 50, 50, 50, 50, 50, 51, 50},
               {50, 51, 50, 50, 50, 50, 50, 51, 50},
               {50, 51, 50, 50, 50, 50, 50, 51, 50},
               {50, 51, 50, 50, 50, 50, 50, 51, 50},
               {50, 51, 51, 51, 51, 51, 51, 51, 50},
               {50, 51, 50, 50, 50, 50, 50, 51, 50},
               {50, 51, 53, 53, 55, 53, 53, 51, 50},
               {50, 50, 50, 50, 50, 50, 50, 50, 50},
               {50, 50, 50, 50, 50, 50, 50, 50, 50}
            }
        };
	private int diem = 9;
	public int getDiem() {
		if (this.TrangThai != 0)
        {
			if(this.TrangThai == 1)
				diem = 90;
//            if (this.Phe == 0)
//                diem += this.score[0][this.Hang][this.Cot];
//            else
//                diem += this.score[1][this.Hang][this.Cot];
        }
        else
            diem = 0;
		return diem;
	}
	
	@Override
	public int KiemTra(int i, int j) {
		boolean turn = true;
        int count = 0;

        if (i < 0 || i > 9 || j < 0 || j > 8) turn = false;
        if (i != Hang && j != Cot) turn = false;
        if (i >= 0 && i <= 9 && j >= 0 && j <= 8 && (i == Hang || j == Cot))
        {
            if (BanCo.ViTri[i][j].Trong == true)
            {
                if (i == Hang && j >= 0 && j <= 8)
                {
                    if (j > Cot)
                        for (int k = Cot + 1; k <= j; k++)
                        {
                            if (BanCo.ViTri[i][k].Trong == false)
                            {
                                turn = false;
                                break;
                            }
                        }
                    if (j < Cot)
                        for (int k = j; k <= Cot - 1; k++)
                        {
                            if (BanCo.ViTri[i][k].Trong == false)
                            {
                                turn = false;
                                break;
                            }
                        }
                }
                if (j == Cot && i >= 0 && i <= 9)
                {
                    if (i > Hang)
                        for (int k = Hang + 1; k <= i; k++)
                        {
                            if (BanCo.ViTri[k][j].Trong == false)
                            {
                                turn = false;
                                break;
                            }
                        }
                    if (i < Hang)
                        for (int k = i; k <= Hang - 1; k++)
                        {
                            if (BanCo.ViTri[k][j].Trong == false)
                            {
                                turn = false;
                                break;
                            }
                        }
                }
            }
            if (BanCo.ViTri[i][j].Trong == false)
            {
                if (BanCo.ViTri[i][j].Phe != this.Phe)
                {
                    if (i == Hang && j >= 0 && j <= 8)
                    {
                        if (j > Cot)
                            for (int k = Cot + 1; k < j; k++)
                            {
                                if (BanCo.ViTri[i][k].Trong == false) count++;

                            }
                        if (j < Cot)
                            for (int k = j + 1; k <= Cot - 1; k++)
                            {
                                if (BanCo.ViTri[i][k].Trong == false) count++;
                            }
                    }
                    if (j == Cot && i >= 0 && i <= 9)
                    {
                        if (i > Hang)
                            for (int k = Hang + 1; k < i; k++)
                            {
                                if (BanCo.ViTri[k][j].Trong == false) count++;

                            }
                        if (i < Hang)
                            for (int k = i + 1; k <= Hang - 1; k++)
                            {
                                if (BanCo.ViTri[k][j].Trong == false) count++;
                            }
                    }
                    if (count != 1) turn = false;
                }
                if (BanCo.ViTri[i][j].Phe == this.Phe) turn = false;
            }                
        }
        //Kiểm tra chống tướng
         if(VanCo.state)
         {
         	if (VanCo.n1.qTuong.Cot == VanCo.n2.qTuong.Cot && Cot == VanCo.n2.qTuong.Cot)
            {
               int ct = 0;
               if (j != Cot)
               {
                   VanCo.OCoTrong(Hang, Cot);
                   for (int t = VanCo.n1.qTuong.Hang + 1; t < VanCo.n2.qTuong.Hang; t++)
                   {
                	   if (BanCo.ViTri[t][Cot].Trong == false) ct++;
                   }
                   if (ct == 0) turn = false;
                   BanCo.ViTri[Hang][Cot].Trong = false;
                   BanCo.ViTri[Hang][Cot].Ten = this.Ten;
                   BanCo.ViTri[Hang][Cot].Phe = this.Phe;
                   BanCo.ViTri[Hang][Cot].ThuTu = this.ThuTu;
               }
               if (j == Cot && (i < VanCo.n1.qTuong.Hang || i > VanCo.n2.qTuong.Hang))
               {
                   VanCo.OCoTrong(Hang, Cot);
                   for (int t = VanCo.n1.qTuong.Hang + 1; t < VanCo.n2.qTuong.Hang; t++)
                   {
                       if (BanCo.ViTri[t][Cot].Trong == false) ct++;
                   }
                   if (ct == 0) turn = false;
                   BanCo.ViTri[Hang][Cot].Trong = false;
                   BanCo.ViTri[Hang][Cot].Ten = this.Ten;
                   BanCo.ViTri[Hang][Cot].Phe = this.Phe;
                   BanCo.ViTri[Hang][Cot].ThuTu = this.ThuTu;
               }
            }
         }else{
         		if (VanCo.n1.qTuong.Cot == VanCo.computer.qTuong.Cot && Cot == VanCo.computer.qTuong.Cot)
                 {
                     int ct = 0;
                     if (j != Cot)
                     {
                         VanCo.OCoTrong(Hang, Cot);
                         for (int t = VanCo.computer.qTuong.Hang + 1; t < VanCo.n1.qTuong.Hang; t++)
                         {
                             if (BanCo.ViTri[t][Cot].Trong == false) ct++;
                         }
                         if (ct == 0) turn = false;
                         BanCo.ViTri[Hang][Cot].Trong = false;
                         BanCo.ViTri[Hang][Cot].Ten = this.Ten;
                         BanCo.ViTri[Hang][Cot].Phe = this.Phe;
                         BanCo.ViTri[Hang][Cot].ThuTu = this.ThuTu;
                     }
                     if (j == Cot && (i < VanCo.n1.qTuong.Hang || i > VanCo.computer.qTuong.Hang))
                     {
                         VanCo.OCoTrong(Hang, Cot);
                         for (int t = VanCo.computer.qTuong.Hang + 1; t < VanCo.n1.qTuong.Hang; t++)
                         {
                             if (BanCo.ViTri[t][Cot].Trong == false) ct++;
                         }
                         if (ct == 0) turn = false;
                         BanCo.ViTri[Hang][Cot].Trong = false;
                         BanCo.ViTri[Hang][Cot].Ten = this.Ten;
                         BanCo.ViTri[Hang][Cot].Phe = this.Phe;
                         BanCo.ViTri[Hang][Cot].ThuTu = this.ThuTu;
                     }
                 }
         }
         
        if(turn) return 1;
        else return 0;
	}
	
	@Override
	public int TuongAnToan(int i, int j) {
		boolean turn = true;
        //Kiểm tra tướng an toàn
        if (turn == true)
        {
            int ho, co;
            QuanCo temp = null;
            temp = new QuanCo();

            ho = this.Hang;
            co = this.Cot;
            if(VanCo.state)
            {
            	if (BanCo.ViTri[i][j].Trong == false)
                {
            		if(BanCo.ViTri[i][j].Phe == VanCo.n1.Phe){
            			for(int k = 0; k < VanCo.n1.dsQuanCo.size(); k++)
                		{
                			if(VanCo.n1.dsQuanCo.get(k).Ten == BanCo.ViTri[i][j].Ten && VanCo.n1.dsQuanCo.get(k).ThuTu == BanCo.ViTri[i][j].ThuTu)
                				temp = VanCo.n1.dsQuanCo.get(k);
                		}	
            		}else {
            			for(int k = 0; k < VanCo.n2.dsQuanCo.size(); k++)
                		{
                			if(VanCo.n2.dsQuanCo.get(k).Ten == BanCo.ViTri[i][j].Ten && VanCo.n2.dsQuanCo.get(k).ThuTu == BanCo.ViTri[i][j].ThuTu)
                				temp = VanCo.n2.dsQuanCo.get(k);
                		}	
					}
                }
            }else{
            	if(BanCo.ViTri[i][j].Trong == false)
            	{
            		if(BanCo.ViTri[i][j].Phe == VanCo.computer.Phe){
            			for(int k = 0; k < VanCo.computer.dsQuanCo.size(); k++)
                		{
                			if(VanCo.computer.dsQuanCo.get(k).Ten == BanCo.ViTri[i][j].Ten && VanCo.computer.dsQuanCo.get(k).ThuTu == BanCo.ViTri[i][j].ThuTu)
                				temp = VanCo.computer.dsQuanCo.get(k);
                		}	
            		}else {
            			for(int k = 0; k < VanCo.n1.dsQuanCo.size(); k++)
                		{
                			if(VanCo.n1.dsQuanCo.get(k).Ten == BanCo.ViTri[i][j].Ten && VanCo.n1.dsQuanCo.get(k).ThuTu == BanCo.ViTri[i][j].ThuTu)
                				temp = VanCo.n1.dsQuanCo.get(k);
                		}	
    				}
            	}
            }
            //Giả sử quân cờ được đi để kiểm tra Tướng mình có bị chiếu ko
            VanCo.OCoTrong(Hang, Cot);
            BanCo.ViTri[i][j].Trong = false;
            BanCo.ViTri[i][j].Phe = Phe;
            BanCo.ViTri[i][j].Ten = Ten;
            BanCo.ViTri[i][j].ThuTu = ThuTu;
            this.Hang = i;
            this.Cot = j;
            if (temp.Phe != 2)
            {
                temp.TrangThai = 0;
//                temp.imgUnit.setVisibility(ImageView.INVISIBLE);
            }
            //Kiểm tra
            if(VanCo.state)
            {
            	if(this.Phe == VanCo.n1.Phe)
            	{
            		if (VanCo.ChieuTuong(VanCo.n1.qTuong) == true) turn = false;
            	}else{
            		if (VanCo.ChieuTuong(VanCo.n2.qTuong) == true) turn = false;
            	}
            }else {
            	if(this.Phe == VanCo.n1.Phe)
            	{
            		if (VanCo.ChieuTuong(VanCo.n1.qTuong) == true) turn = false;
            	}else{
            		if (VanCo.ChieuTuong(VanCo.computer.qTuong) == true) turn = false;
            	}
			}
            //Trả lại những gì đã giả sử ở trên ^^!
            this.Hang = ho;
            this.Cot = co;
            VanCo.OCoTrong(i, j);
            BanCo.ViTri[ho][co].Trong = false;
            BanCo.ViTri[ho][co].Phe = Phe;
            BanCo.ViTri[ho][co].Ten = Ten;
            BanCo.ViTri[ho][co].ThuTu = ThuTu;
            if (temp.Phe != 2)
            {
            	if(temp.isFirst && temp.Ten != "tuong")
					temp.TrangThai = 2;
				else
					temp.TrangThai = 1;
                temp.imgUnit.setVisibility(ImageView.VISIBLE);
                BanCo.ViTri[i][j].Trong = false;
                BanCo.ViTri[i][j].Ten = temp.Ten;
                BanCo.ViTri[i][j].Phe = temp.Phe;
                BanCo.ViTri[i][j].ThuTu = temp.ThuTu;
                BanCo.ViTri[i][j].TrangThai = temp.TrangThai;
            }
        }
        //Trả về kết quả
        if (turn) return 1;
        else return 0;
    }
	
	
}
